﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq;
using WX.WMS.DataModel;
using WX.WMS.Common;
using WX.WMS.BLL;

public partial class Product_UnitManager : System.Web.UI.Page
{
    private string action
    {
        get
        {
            if (ViewState["action"] == null)
                ViewState["action"] = "";
            return ViewState["action"].ToString();
        }
        set { ViewState["action"] = value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            init();
        }

    }
    protected void init()
    {
        action = Request.QueryString["action"];

        CommomMethod.BindEnumForDropdwonList(this.ddlType, typeof(Enumerations.UnitType));

        if (action != null)
        {
            if (action.ToString().Equals("edit"))
            {
                string ID = Request.QueryString["ID"].ToString();
                UnitBLL unitBLL = new UnitBLL();
                T_UNIT unit = unitBLL.GetUnitByID(ID);

                this.txtUnitName.Text = unit.NAME;
                ddlType.Enabled = false;

                updateButton.Text = "修改";

            }
            else if (action.ToString().Equals("del"))
            {
                string ID = Request.QueryString["ID"].ToString();
                UnitBLL unitBLL = new UnitBLL();
                ddlType.Enabled = true;
                unitBLL.DeleteUnit(ID);
            }
        }
        else
        {

            updateButton.Text = "添加";
            ddlType.Enabled = true;
        }

        DataBand();

    }

    protected void DataBand()
    {
        UnitBLL unitBLL = new UnitBLL();
        SearchResult.DataSource = unitBLL.GetUnitList();
        SearchResult.DataBind();
    }

    protected void updateButton_Click(object sender, EventArgs e)
    {
        ddlType.Enabled = true;
        UnitBLL unitBLL = new UnitBLL();
        if (unitBLL.IsExisted(Convert.ToInt32(this.ddlType.SelectedValue), this.txtUnitName.Text.Trim()))
        {
            if (action == "edit")
            {
                string ID = Request.QueryString["ID"].ToString();
                T_UNIT unit = unitBLL.GetUnitByID(ID);
                if (txtUnitName.Text.Trim() != unit.NAME)
                {
                    Jscript.AjaxAlert(this, "单位名称已存在，请重新输入！");
                    return;
                }
            }
            else
            {
                Jscript.AjaxAlert(this, "单位名称已存在，请重新输入！");
                return;
            }
        }
        
        if (action == "edit")
        {
            string ID = Request.QueryString["ID"].ToString();

            T_UNIT unit = unitBLL.GetUnitByID(ID);
            unit.NAME = this.txtUnitName.Text.Trim();
            unit.Type = Convert.ToInt32(this.ddlType.SelectedValue);
            unitBLL.UpdateUnit(unit);

            action = null;
            updateButton.Text = "添加";
            this.txtUnitName.Text = "";
        }
        else
        {
            T_UNIT unit = new T_UNIT();
            unit.NAME = this.txtUnitName.Text.Trim();
            unit.Type = Convert.ToInt32(this.ddlType.SelectedValue);
            unitBLL.AddUnit(unit);
        }

        DataBand();
    }
    protected void SearchResult_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        SearchResult.PageIndex = e.NewPageIndex;
        action = null;
        this.txtUnitName.Text = "";
        init();
    }
}
